Multilayer Perceptron with Keras


In [3]:
# Import MINST data
from keras.datasets import mnist
from keras import backend as K
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation 
from keras.optimizers import Adam

In [4]:
# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Parameters
learning_rate = 0.001
training_epochs = 15
batch_size = 100

# Network Parameters
n_hidden_1 = 256 # 1st layer number of features
n_hidden_2 = 256 # 2nd layer number of features
n_input = 784 # MNIST data input (img shape: 28*28)
n_classes = 10 # MNIST total classes (0-9 digits)

X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, n_classes)
Y_test = np_utils.to_categorical(y_test, n_classes)

model = Sequential()
model.add(Dense(n_hidden_1, input_shape=(784,)))
model.add(Activation('relu'))
model.add(Dense(n_hidden_2))
model.add(Activation('relu'))   
model.add(Dense(n_classes))
model.add(Activation('softmax'))
      
adam = Adam(lr=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(loss='categorical_crossentropy',
              optimizer=adam,
              metrics=['accuracy'])

model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=training_epochs,
          verbose=1, validation_data=(X_test, Y_test))
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])


(60000, 'train samples')
(10000, 'test samples')
Train on 60000 samples, validate on 10000 samples
Epoch 1/15
60000/60000 [==============================] - 10s - loss: 0.2516 - acc: 0.9261 - val_loss: 0.1262 - val_acc: 0.9608
Epoch 2/15
60000/60000 [==============================] - 10s - loss: 0.0997 - acc: 0.9691 - val_loss: 0.0929 - val_acc: 0.9700
Epoch 3/15
60000/60000 [==============================] - 12s - loss: 0.0653 - acc: 0.9793 - val_loss: 0.0761 - val_acc: 0.9771
Epoch 4/15
60000/60000 [==============================] - 11s - loss: 0.0453 - acc: 0.9853 - val_loss: 0.0766 - val_acc: 0.9768
Epoch 5/15
60000/60000 [==============================] - 11s - loss: 0.0378 - acc: 0.9874 - val_loss: 0.0776 - val_acc: 0.9759
Epoch 6/15
60000/60000 [==============================] - 11s - loss: 0.0285 - acc: 0.9906 - val_loss: 0.0713 - val_acc: 0.9797
Epoch 7/15
60000/60000 [==============================] - 13s - loss: 0.0252 - acc: 0.9920 - val_loss: 0.0767 - val_acc: 0.9770
Epoch 8/15
60000/60000 [==============================] - 11s - loss: 0.0181 - acc: 0.9943 - val_loss: 0.0748 - val_acc: 0.9787
Epoch 9/15
60000/60000 [==============================] - 10s - loss: 0.0169 - acc: 0.9941 - val_loss: 0.0851 - val_acc: 0.9772
Epoch 10/15
60000/60000 [==============================] - 10s - loss: 0.0156 - acc: 0.9947 - val_loss: 0.0922 - val_acc: 0.9783
Epoch 11/15
60000/60000 [==============================] - 9s - loss: 0.0146 - acc: 0.9950 - val_loss: 0.1056 - val_acc: 0.9762
Epoch 12/15
60000/60000 [==============================] - 10s - loss: 0.0141 - acc: 0.9953 - val_loss: 0.0762 - val_acc: 0.9803
Epoch 13/15
60000/60000 [==============================] - 12s - loss: 0.0102 - acc: 0.9966 - val_loss: 0.0932 - val_acc: 0.9779
Epoch 14/15
60000/60000 [==============================] - 11s - loss: 0.0125 - acc: 0.9959 - val_loss: 0.1049 - val_acc: 0.9779
Epoch 15/15
60000/60000 [==============================] - 12s - loss: 0.0122 - acc: 0.9961 - val_loss: 0.0865 - val_acc: 0.9802
('Test loss:', 0.086470205308183445)
('Test accuracy:', 0.98019999999999996)

In [ ]: